package main

import (
	"elegantGo/chapter17/controller"
	"elegantGo/chapter17/repository"
	"elegantGo/chapter17/repository/ent"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	fieldmaskutils "github.com/mennanov/fieldmask-utils"
	"log"
)

func main() {

	mask1 := fieldmaskutils.MaskFromString("ID,Content,CreateTime,User{ID,Nickname,Avatar},Article{ID,Title,CommentCount,CreateTime,User{ID,Nickname,Avatar}}")
	m, ok := mask1.Get("Article")

	fmt.Println(m, ok) // 输出: a,b{c,d}
	fmt.Println(m.Get("User"))

	//mask := fieldmask_utils.MaskFromString("Username,Email")

	// 目标 Map

	//type Comment struct {
	//	ID         int
	//	Content    string
	//	CreateTime time.Time
	//}
	//
	//comment := &Comment{
	//	ID:         789,
	//	Content:    "bob",
	//	CreateTime: time.Now(),
	//}
	//
	//mask := fieldmaskutils.MaskFromString("ID")
	//var commentDst Comment
	//
	//if err := fieldmaskutils.StructToStruct(mask, comment, &commentDst); err != nil {
	//	panic(err)
	//}
	//
	//fmt.Printf("复制到 Map 的结果: %+v\n", commentDst)
	//
	//return

	db, err := ent.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/blog?charset=utf8mb4&parseTime=true&loc=Local")
	if err != nil {
		log.Fatalf("ent.Open: %s", err)
	}

	srv := gin.Default()

	repoArticle := repository.NewArticle(db)
	repoUser := repository.NewUser(db)
	repoVip := repository.NewVip(db)

	ctrArticle := controller.NewArticle(repoArticle, repoUser)
	ctrUser := controller.NewUser(repoUser)
	ctrVip := controller.NewVip(repoVip)

	handler := new(controller.Handler)

	srv.GET("/article", handler.Wrapper(ctrArticle.One))
	srv.GET("/articles", handler.Wrapper(ctrArticle.Many))
	srv.POST("/article/create", handler.Wrapper(ctrArticle.Create))

	srv.GET("/user", handler.Wrapper(ctrUser.One))
	srv.GET("/users", handler.Wrapper(ctrUser.Many))

	srv.GET("/vips", handler.Wrapper(ctrVip.Many))

	srv.Run(":80")
}
